<?php
/**
 * @author Pham Van Diep
 * @copyright 2014
 * @description: save national
 */
// not login
// not perm add
if(!isset($AppUI) ||
   !isset($_SESSION['personal'])||
	(isset($_SESSION['personal']) && ($_SESSION['personal']&1<<1) == 0)// ||
//    !isset($_GET['personalid'])

) {
    redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
}
// data input
//var_dump($_POST); die();
$arrErr = array();
$intClubId = intval($_POST['clubid']);

$strName            = $this->input->post('name',null); 
$intLen = strlen($strName);
if ($intLen < VL_NAME_MINLENGTH || $intLen > VL_NAME_MAXLENGTH ) {
    array_push($arrErr, $arrLocale['name_range']);
}
$strShort    	    = $this->input->post('sshort',null);
$intLen = strlen($strShort);
if ($intLen < VL_SHORT_MINLENGTH || $intLen > VL_SHORT_MAXLENGTH ) {
    array_push($arrErr, $arrLocale['short_range']);
}			
$strDes             = $this->input->post('description',null);
$intLen = strlen($strDes);
if ($intLen < VL_CONTENT_MINLENGTH ) {
    array_push($arrErr, $arrLocale['description_minlength']);
}
$strTag             = $this->input->post('tag', null);
$link = $this->input->post('link',null);
if ($link == null) {
    if($_FILES['logo']['error'] > 0) {
        array_push($arrErr, $arrLocale['logo_upload']);
    }else{
        $this->load->library('upload');
        $arrInfo = $this->upload->do_upload('logo');
    }
}else{
    $this->load->library('upload');
    $arrInfo = $this->upload->do_upload_url($link);
}
if(!isset($arrInfo) || count($arrInfo) == 0) {
    array_push($arrErr, $arrLocale['logo_upload']);
}
if(count($arrErr)==0) {//allow save          
    $strLogo = CI_Function::generate($arrInfo['path'].$arrInfo['firstname'].'.'.$arrInfo['extension'], IMAGE_LOGO_SIZE,IMAGE_LOGO_SIZE);
    
   // $strLogo = CI_Function::getRootImageUrl($arrInfo['path'],$arrInfo['firstname'],$arrInfo['extension']);          
    $intError = $this->sponsor->add($AppUI->id,$intClubId,$strName, CI_Function::getUrl($strLogo), $strShort,$strDes, $strTag);    
    if($intError != 0) {        
        array_push($arrErr,$arrLocale['sys_add_err']);        
        $_SESSION['SPONSOR_ERROR'] = $arrErr;                
    } else{
        if(isset($_SESSION['SPONSOR_ERROR']) ) unset($_SESSION['SPONSOR_ERROR']);
    }          
}
// occur error

if (count($arrErr) !== 0) {       
    $arrInput = array(
            'name'  => $strName,
            'sshort'=> $strShort,
            'link'  =>$link,
            'description'=>$strDes,
            'tag'   => $strTag
    );    
    $_SESSION['SPONSOR_ERROR'] = $arrErr;
    $this->smarty->assign('arrInput',$arrInput);
    $this->smarty->display('sponsor/add.tpl');
    //redirect(HOST_BACKEND . 'sponsor/add?clubid='.$intClubId);
}else{
    redirect(isset($_SESSION[SESSION_BACKURL]) ? $_SESSION[SESSION_BACKURL] : HOST_BACKEND);
}
